/*
 * Copyright (c) 2018. czmc All Rights Reserved.
 * Author: czmc
 * Email: zxc6355533@163.com
 * Encoding: UTF-8
 */

package top.stime21.myneighbours.module.login

import android.os.Bundle
import com.tuo.customview.VerificationCodeView
import kotlinx.android.synthetic.main.activity_check_sms.*
import kotlinx.android.synthetic.main.include_header.*
import org.greenrobot.eventbus.EventBus
import top.stime21.framework.extension.toActivity
import top.stime21.myneighbours.Events
import top.stime21.myneighbours.R
import top.stime21.myneighbours.base.BuzMvpActivity
import top.stime21.myneighbours.module.login.presenter.LoginPrensenterImpl
import top.stime21.myneighbours.module.main.MainActivity


class CheckSmsActivity : BuzMvpActivity<LoginContract.Presenter>(), LoginContract.View {
    override fun onLoginSuccess() {
        toActivity<MainActivity>()
        EventBus.getDefault().post(Events.Login())
        finish()
    }

    override fun onSMSCount(second: Long, done: Boolean) {
        if (done) {
            btn_resend.isEnabled = true
            btn_resend.text = "重新获取验证码"
        } else {
            btn_resend.isEnabled = false
            btn_resend.text = "${second}s后重新发送"
        }
    }

    override fun getPresenter(): LoginContract.Presenter {
        return LoginPrensenterImpl(this)
    }

    private var phone: String? = null
    private var inputcount = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_check_sms)
        phone = intent.getSerializableExtra("data") as String
        tv_title.text = "快捷登陆"
        tv_tips.text = "验证码已发送至 " + phone
        mPrensenter!!.sendSms(phone!!)
        btn_resend.setOnClickListener({
            mPrensenter!!.sendSms(phone!!)
        })
        et_checkcode.setInputCompleteListener(onCompleteListener)

    }

    private val onCompleteListener: VerificationCodeView.InputCompleteListener = object : VerificationCodeView.InputCompleteListener {
        override fun inputComplete() {
            inputcount++
            if (inputcount == 6) {
                mPrensenter!!.login(phone!!,"${et_checkcode.inputContent}")
            }
        }

        override fun deleteContent() {
            inputcount--
        }

    }

    override fun onDestroy() {
        super.onDestroy()
    }

}